package com.gyg.bro.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Objects;

@Slf4j
public class FileUtil {

    /**
     * 生成文件大小
     * @param size
     * @return
     */
    public static String getSize(Long size) {
        Long KB = 1024L;
        Long MB = 1024 * KB;
        Long GB = 1024 * MB;
        Long TB = 1024 * GB;

        if (Objects.isNull(size)) {
            return "未知";
        }

        if (size <= KB) {
            return size + "B";
        }else if (size < MB) {
            return new BigDecimal(size).divide(new BigDecimal(KB), 2, BigDecimal.ROUND_HALF_UP) + "KB";
        }else if (size.equals(MB)) {
            return new BigDecimal(size).divide(new BigDecimal(KB), 0, BigDecimal.ROUND_HALF_UP) + "KB";
        }else if (size < GB) {
            return new BigDecimal(size).divide(new BigDecimal(MB), 2, BigDecimal.ROUND_HALF_UP) + "MB";
        }else if (size.equals(GB)) {
            return new BigDecimal(size).divide(new BigDecimal(MB), 0, BigDecimal.ROUND_HALF_UP) + "MB";
        }else if (size < TB) {
            return new BigDecimal(size).divide(new BigDecimal(GB), 2, BigDecimal.ROUND_HALF_UP) + "GB";
        }else if (size.equals(TB)) {
            return new BigDecimal(size).divide(new BigDecimal(GB), 0, BigDecimal.ROUND_HALF_UP) + "GB";
        }else {
            return new BigDecimal(size).divide(new BigDecimal(TB), 2, BigDecimal.ROUND_HALF_UP) + "TB";
        }
    }

    public static void main(String[] args) {
        String size = getSize(1073741824L);
        System.out.println(size);
    }

}
